package com.hoshiicloud.saleorder.service.impl.refund;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.saleorder.entity.Consultations;
import com.hoshiicloud.saleorder.feign.IdGeneratorService;
import com.hoshiicloud.saleorder.mapper.refund.ConsultationsMapper;
import com.hoshiicloud.saleorder.service.refund.ConsultationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 协商历史(Consultations)表服务实现类
 *
 * @author qry
 * @since 2019-08-22 11:00:16
 */
@Service
public class ConsultationsServiceImpl extends ServiceImpl<ConsultationsMapper, Consultations> implements ConsultationsService {
    @Autowired
    private IdGeneratorService idGeneratorService;

    @Override
    @Logable(authName = "退款管理-退款协商历史表操作-根据ID查询退款协商历史信息")
    @Cacheable(cacheNames = "Consultations", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    public Consultations getConsultationsById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    @CacheEvict(cacheNames = {"Consultations"}, allEntries = true)
    @Logable(authName = "退款管理-退款协商历史表操作-新增退款协商历史")
    public Consultations saveConsultations(Consultations entity) {
        LocalDateTime now = LocalDateTime.now();
        entity.setCreatedTime(now);
        entity .setUpdatedTime(now);
        entity .setId(this.getNewId());
        int i = baseMapper.insert(entity);
        if(i == 0){
            throw new ServiceException("生成退款协商历史失败，请重试");
        }
        return entity;
    }

    @Override
    @Logable(authName = "退款管理-退款协商历史表操作-根据退款单ID查询退款协商历史信息")
    @Cacheable(cacheNames = "Consultations", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#refundId")
    public List<Consultations> listByRefundId(Long refundId) {
        QueryWrapper<Consultations> q = new QueryWrapper<>();
        q.lambda().eq(Consultations::getRefundId,refundId);
        return baseMapper.selectList(q);
    }

    @Override
    @CacheEvict(cacheNames = {"Consultations"}, allEntries = true)
    @Logable(authName = "退款管理-退款协商历史表操作-删除退款协商历史")
    public Boolean physicsDelete(Long refundId) {
        baseMapper.physicsDelete(refundId);
        return true;
    }

    @Override
    @Logable(authName = "退款管理-退款协商历史表操作-根据退款单ID集合查询退款协商历史信息")
    @Cacheable(cacheNames = "Consultations", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    public List<Consultations> listByRefundIds(List<Long> refundIds, String key) {
        QueryWrapper<Consultations> q = new QueryWrapper<>();
        q.lambda().in(Consultations::getRefundId,refundIds).orderByAsc(Consultations::getRefundId);
        return baseMapper.selectList(q);
    }


    private Long getNewId(){
        String id = idGeneratorService.queryGeneratorId();
        if (StrUtil.isBlank(id)) {
            throw new ServiceException(CommonError.error("主键ID生成失败"));
        }
        return Long.valueOf(id);
    }
}
